﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Command;
using JustinAngel.Neurons.AppCore;
using JustinAngel.Neurons.Framework;
using JustinAngel.Neurons.Framework.Navigation;
using JustinAngel.Neurons.Pages.Main;
using Microsoft.Phone.Shell;

namespace JustinAngel.Neurons.Pages.Splash
{
    public class SplashViewModel : VViewModelBase
    {
        private int _numberOfLoadedEvents = 0;
        public ICommand Loaded
        {
            get
            {
                return new RelayCommand(() =>
                {
                    if (_numberOfLoadedEvents != 0) return;

                    SplashVideoSource = new Uri("/NeuronsSplash.wmv", UriKind.Relative);
                    IsSplashPlaying = true;
                    DispatcherTimerHelper.On(TimeSpan.FromSeconds(12), MoveToNextPage);

                    _numberOfLoadedEvents++;
                });
            }
        }

        private void MoveToNextPage()
        {
            //SplashVideoSource = null;

            if (ZuneTileClickOnVideoItem == null)
                MessengerInstance.Send(new NavigateToMessage(PageSources.MainPage));
            else
                MessengerInstance.Send(new NavigateToMessage(PageSources.VideoPlayerPage, ZuneTileClickOnVideoItem));


        }


        private bool _isSplashPlaying;
        public bool IsSplashPlaying
        {
            get { return _isSplashPlaying; }
            set
            {
                _isSplashPlaying = value;
                RaisePropertyChangedAndUpdateVSM(() => IsSplashPlaying, value);
            }
        }

        private Uri _splashVideoSource;
        public Uri SplashVideoSource
        {
            get { return _splashVideoSource; }
            set
            {
                _splashVideoSource = value;
                RaisePropertyChanged(() => SplashVideoSource);
            }
        }

        public VideoItemViewModel ZuneTileClickOnVideoItem { get; set; }


        private bool WasNavigatedTo
        {
            get
            {
                if (!PhoneApplicationService.Current.State.ContainsKey("SplashViewModel.WasNavigatedTo"))
                {
                    PhoneApplicationService.Current.State["SplashViewModel.WasNavigatedTo"] = false;
                }
                return (bool)PhoneApplicationService.Current.State["SplashViewModel.WasNavigatedTo"];
            }
            set
            {
                PhoneApplicationService.Current.State["SplashViewModel.WasNavigatedTo"] = value;
            }
        }

        public void OnNavigatedTo()
        {
            if (!WasNavigatedTo)
            {
                WasNavigatedTo = true;
            }
            else
            {
                throw new ApplicationExitException();
            }
        }

        public ICommand OnUserInteractionWithSplashShowLoadingAnimation
        {
            get
            {
                return new RelayCommand(() => IsLoading = true);
            }
        }

        private bool _isLoading;
        public bool IsLoading
        {
            get { return _isLoading; }
            set
            {
                _isLoading = value;
                RaisePropertyChangedAndUpdateVSM(() => IsLoading, value);
            }
        }
    }
}
